#z自定义模板过滤器
from  django import template
from apps.blog.models import Post
register = template.Library()
@register.simple_tag#将函数注册成简单的标签,这里将默认使用函数名称
#@register.simple_tag(name='设置标签名')
def total_posts():
    return  Post.published.count()
@register.inclusion_tag('blog/post/latest_post.html')
def show_latest_posts(count=5):
    latest_posts=Post.published.order_by('-publish')[:count]
    return {'latest_posts':latest_posts}

from django.db.models import Count
@register.simple_tag
def get_most_commented_posts(count=5):
    return Post.published.annotate(
        total_comments=Count('comments')
    ).order_by('-total_comments')[:count]


import markdown
@register.filter(name='markdown')
def markdown_format(text):
    return markdown.markdown(text,
                             extensions=[
                                 # 包含 缩写、表格等常用扩展
                                 'markdown.extensions.extra',
                                 # 语法高亮扩展
                                 'markdown.extensions.codehilite',
                                 # 允许我们自动生成目录
                                 'markdown.extensions.toc',
                             ])



